package com.example.hallserver.controller;

import com.example.common.ChessTypeEnum;
import com.example.common.MjDemo;
import com.example.common.User;
import com.example.hallserver.UserClient;
import com.example.hallserver.config.ChessConfig;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created on 2020/2/17.
 *
 * @author Grak
 * @since 1.0
 */
@RestController
@DefaultProperties(defaultFallback = "fallback")
public class ChessController {

    @Autowired
    private ChessConfig config;

    @Autowired
    private UserClient userClient;

    @Value("${server.port}")
    private String port;

    @GetMapping("/getChess")
    @HystrixCommand
    public String getChess(){
        return "port="+ port + "," + config.toString();
    }

    @GetMapping("/getChessOnly")
    public MjDemo getChessOnly(){
        MjDemo user = new MjDemo();
        user.setName(config.getName());
        user.setColor(2);
        user.setType(ChessTypeEnum.CHESS_START);
        user.setAllChessNum(Integer.valueOf(config.getAge()));
        return user;
    }

    @GetMapping("/getChessByHall")
    public User getChessByHall(){
        String name = userClient.getUser();
        return userClient.getUserOnly();
    }

    private String fallback() {
        return "太拥挤了, 请稍后再试~~" + port;
    }
}
